package com.kc.saf.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kc.saf.domain.CoreType;
import com.kc.saf.domain.SafetyAlertType;
import com.kc.saf.service.CoreTypeService;
import com.kc.saf.service.SafetyAlertTypeService;
import com.kc.saf.vo.CoreTypeVo;
import com.kc.saf.vo.SafetyAlertTypeVo;
import com.kc.sys.common.DataGridView;
import com.kc.sys.common.R;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 老杜
 * @since 2019-11-08
 */
@RestController
@RequestMapping("/coreType")
public class CoreTypeController {

    @Autowired
    private CoreTypeService coreTypeService;
    @Autowired
    private SafetyAlertTypeService safetyAlertTypeService;

    /**
     * 查询
     */
    @RequestMapping("loadAllCoreType")
    public DataGridView loadAllCoreType(CoreTypeVo coreTypeVo){
        //获取用户session
        //User user = (User)WebUtils.getSession().getAttribute("user");
        IPage<CoreType> page = new Page<>(coreTypeVo.getPage(),coreTypeVo.getLimit());
        QueryWrapper<CoreType> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(coreTypeVo.getSafetyId()!=null,"safety_id",coreTypeVo.getSafetyId());
        queryWrapper.like(StringUtils.isNotBlank(coreTypeVo.getTypename()),"typename",coreTypeVo.getTypename());
        queryWrapper.orderByAsc("safety_id").orderByAsc("createtime");
        coreTypeService.page(page,queryWrapper);
        List<CoreType> records = page.getRecords();
        for (CoreType record : records) {
            SafetyAlertType safetyAlertType = safetyAlertTypeService.getById(record.getSafetyId());
            record.setSafetyName(safetyAlertType.getTypename());
        }
        return new DataGridView(page.getTotal(),page.getRecords());
    }
    //添加公告
    @RequestMapping("addCoreType")
    public R addCoreType(CoreTypeVo coreTypeVo){
        try {
            coreTypeVo.setCreatetime(new Date());
            coreTypeService.save(coreTypeVo);
            return R.ok().message("添加成功");
        } catch (Exception e) {
            e.printStackTrace();
            return R.error().message("添加失败");
        }
    }
    //更新公告
    @RequestMapping("updateCoreType")
    public R updateCoreType(CoreTypeVo coreTypeVo){
        try {
            coreTypeService.updateById(coreTypeVo);
            return R.ok().message("更新成功");
        } catch (Exception e) {
            e.printStackTrace();
            return R.error().message("更新失败");
        }
    }
    /**
     * 根据Id 删除
     * */
    @RequestMapping("deleteCoreType")
    public R deleteById(Integer id){
        try {
            coreTypeService.removeById(id);
            return R.ok().message("删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return R.error().message("删除失败");
        }
    }
    /**
     * 查询条件
     * */
    @RequestMapping("loadAllcoreTypeForSelect")
    public DataGridView loadAllcoreTypeForSelect(){
        List<CoreType> list = coreTypeService.list(null);
        return new DataGridView(list);
    }


    /**
     * 根据警示类型查询文件夹
     * */
    @RequestMapping("loadBySafetyTypeForSelect")
    public DataGridView loadBySafetyTypeForSelect(Integer safetyId){
        QueryWrapper<CoreType> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(safetyId!=null,"safety_id",safetyId);

        List<CoreType> list = coreTypeService.list(queryWrapper);
        return new DataGridView(list);
    }

    /**
     * 根据安全警示类型以及文件类型查看文件夹
     */
    @RequestMapping("loadByBySafetyTypeAndFileType")
    public DataGridView loadByBySafetyTypeAndFileType(Integer safetyId, Integer fileType){

        QueryWrapper<CoreType> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(safetyId!=null,"safety_id",safetyId);
        queryWrapper.eq(fileType!=null,"file_type",fileType);
        queryWrapper.orderByAsc("typename");
        queryWrapper.orderByAsc("createtime");
        List<CoreType> list = coreTypeService.list(queryWrapper);
        if(list.size()<=3){
            return new DataGridView(list);
        }else {
            return new DataGridView(list.subList(0,3));
        }
    }
}

